package com.livepan.server.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.livepan.server.dao.SectionDOMapper;
import com.livepan.server.dataobject.SectionDO;
import com.livepan.server.dataobject.SectionDOExample;
import com.livepan.server.error.BusinessException;
import com.livepan.server.error.EmBusinessError;
import com.livepan.server.service.model.SectionModel;
import com.livepan.server.service.model.PageModel;
import com.livepan.server.service.model.SectionPageModel;
import com.livepan.server.utils.CopyUtil;
import com.livepan.server.utils.UUIDUtil;
import com.livepan.server.utils.ValidatorUtil;
import com.livepan.server.validator.ValidationResult;
import com.livepan.server.validator.ValidatorImpl;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.nio.cs.CharsetMapping;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SectionService {
    @Autowired
    private SectionDOMapper sectionDOMapper;

    @Autowired
    private ValidatorImpl validator;

    @Autowired
    private CourseService courseService;

    public void list(SectionPageModel sectionPageModel) {
        PageHelper.startPage(sectionPageModel.getPage(), sectionPageModel.getSize());
        SectionDOExample sectionDOExample = new SectionDOExample();

        SectionDOExample.Criteria criteria = sectionDOExample.createCriteria();

        if (!StringUtils.isNullOrEmpty(sectionPageModel.getChapterId())) {
            criteria.andChapterIdEqualTo(sectionPageModel.getChapterId());
        }
        if (!StringUtils.isNullOrEmpty(sectionPageModel.getCourseId())) {
            criteria.andCourseIdEqualTo(sectionPageModel.getCourseId());
        }

        List<SectionDO> sectionDOList = sectionDOMapper.selectByExample(sectionDOExample);
        PageInfo<SectionDO> pageInfo = new PageInfo<>(sectionDOList);
        sectionPageModel.setTotal(pageInfo.getTotal());
        List<SectionModel> sectionModelList = sectionDOList.stream().map(
                this::convertSectionModelFromSectionDO
        ).collect(Collectors.toList());
        sectionPageModel.setList(sectionModelList);
    }

    @Transactional(rollbackFor = Exception.class)
    public void save(SectionModel sectionModel) throws Exception {
        ValidationResult validateResult = validator.validate(sectionModel);
        if (validateResult.isHasError()) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, validateResult.getErrMsg());
        }
        SectionDO sectionDO = CopyUtil.copy(sectionModel, SectionDO.class);
        if (sectionDOMapper.selectByPrimaryKey(sectionDO.getId()) == null) {
            this.insert(sectionDO);
        } else {
            this.update(sectionDO);
        }
        courseService.updateTime(sectionDO.getCourseId());
    }

    private void insert(SectionDO sectionDO) {
        sectionDO.setId(UUIDUtil.getShortUUID());
        sectionDOMapper.insert(sectionDO);
    }

    private void update(SectionDO sectionDO) {
        sectionDOMapper.updateByPrimaryKey(sectionDO);
    }

    public void delete(String id) {
        sectionDOMapper.deleteByPrimaryKey(id);
    }


    private SectionModel convertSectionModelFromSectionDO(SectionDO sectionDO) {
        if (sectionDO == null) {
            return null;
        }
        SectionModel sectionModel = new SectionModel();
        BeanUtils.copyProperties(sectionDO, sectionModel);
        return sectionModel;
    }
}
